/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "Hogshead.Common.h"

namespace Hogshead
{
	namespace Core
	{
		class IMessageReceiver;
		class IMessageSender;
		class IMessage;
		class Messenger
		{
		public:
			static void initialize();
			static void finalize();
			static void handleMessages();

			// send the message and have the messenger delete it
			static void sendMessage(IMessage*, IMessageReceiver*);
			// forward the message and don't have the messenger delete it
			static void forwardMessage(IMessage*, IMessageReceiver*);

			/**
			Registers the passed in receiver to receive messages of type T from the passed in sender
			**/
			template<typename T> static void registerReceiver(IMessageReceiver* in_receiver, IMessageSender* in_sender)
			{
				if(in_receiver && in_sender)
				{
					int message_id = T::messageID();
					Vector<IMessageReceiver*>* receiver_list = NULL;
					// see if the sender has this sort of message list yet
					if(in_sender->_receiver_index->tryGetValue(message_id, receiver_list) == false)
					{
						// no list yet for that type of message, so make new one
						receiver_list = new Vector<IMessageReceiver*>();
						in_sender->_receiver_index->add(message_id,receiver_list);
					}
					
					// add receiver to senders list
					receiver_list->add(in_receiver);
					in_receiver->_senders->add(Pair<int,IMessageSender*>(message_id,in_sender));
				}
			}

			/**
			Unregisters the passed in receiver from the passed in sender for messages of type T
			**/
			template<typename T> static void unregisterReceiver(IMessageReceiver* in_receiver, IMessageSender* in_sender)
			{
				// make sure neither receiver or sender are null
				if(in_receiver && in_sender)
				{
					int message_id = T::messageID();
					// first make sure we have a reference to the sender in the receiver and try to remove it
					if(in_receiver->_senders->remove(Pair<int,IMessageSender*>(message_id,in_sender)))
					{
						// receiver had reference to the sender, so it got removed
						// now unregister receiver from the sender
						Vector<IMessageReceiver*>* receiver_list = NULL;
						if(in_sender->_receiver_index->tryGetValue(message_id, receiver_list))
							receiver_list->swapRemove(in_receiver);
					}
				}
			}

			/**
			* This method needs to be called on IMessageSenders when they are going to be deleted
			* removes the sender from all of the receivers that have registered with it
			**/
			static void clearReceivers(IMessageSender* in_sender);
		private:
			friend class IMessageSender;
			friend class IMessageReceiver;

			static Mutex _receiver_buffer_lock;
			static Mutex _receiver_set_lock;
			static Mutex _message_buffer_lock;
			static Mutex _message_set_lock;

			static Vector<IMessageReceiver*>* _receiver_buffer_0;
			static Vector<IMessageReceiver*>* _receiver_buffer_1;
			static HashSet<void*>* _receiver_set_0;
			static HashSet<void*>* _receiver_set_1;
			static ArrayList<IMessage*>* _message_buffer;
			static HashSet<void*>* _message_set;
		};
	}
}